<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comment extends CI_Controller {

	public function index() {}

	public function getCmtData($lesson) {
		$this->load->model('Comments_model');
		$cursor = $this->Comments_model->getPublicComment($lesson);
		$cmtArr = [];
		foreach ($cursor as $value) {
			$strTime = date('d/m/y H:i:s', $value['time']->sec);
			$value['time'] = $strTime;
			array_push($cmtArr, $value);
		}
		echo json_encode($cmtArr);
	}
	public function getUserComment() {
		if(!$this->session->userdata('username')) {
			return false;
		}
		$username = $this->session->userdata('username');

		$this->load->model('Comments_model');
		$cursor = $this->Comments_model->getUserComment($username);
		$cursor->sort(array('time'=>-1));
		$cmtArr = [];
		foreach($cursor as $value) {
			$strTime = date('d/m/y H:i:s', $value['time']->sec);
			$value['time'] = $strTime;

			$this->load->model('Model_lesson');
			$ls = $this->Model_lesson->getLesson($value['lesson']);
			$value['lsname'] = $ls['title'];

			array_push($cmtArr, $value);
		}
		echo json_encode($cmtArr);
	}
	// public function getDelete()
	public function rawComment() {
		$this->load->model('Comments_model');
		$cursor = $this->Comments_model->getRawComment();
		$cmtArr = [];
		foreach ($cursor as $value) {
			$strTime = date('d/m/y H:i:s', $value['time']->sec);
			$value['time'] = $strTime;
			array_push($cmtArr, $value);
		}
		echo json_encode($cmtArr);
	}
	public function deleteCmt($id) {
		$this->load->model('Comments_model');
		$msg = $this->Comments_model->deleteCmt($id);
		echo $msg;
	}
	public function checkCmt($id) {
		$this->load->model('Comments_model');
		$msg = $this->Comments_model->checkCmt($id);
		echo $msg;
	}
	public function addComment() {
		$content = $this->input->post('cmt');
		$lsId = $this->input->post('lsid');
		$parent = $this->input->post('par');
		if($parent == 'null') $parent = null;

		$user = $this->session->userdata('username');
		if($user != null) {
			$this->load->model('Comments_model');
			$cursor = $this->Comments_model->insertComment($content, $lsId, $parent, $user);
			echo $cursor;
		}
		else {
			echo 'notLogin';
		}
	}
}

?>